﻿using CalendarBoard;
using ComClassLibrary;
using KeyboardArg;
using SoftKeyBoard;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Windows.Interop;
using Message = ComClassLibrary.Message;

namespace KeyboardManager
{
    public partial class FormCalender : Form
    {
        private const int MSG_NOTIFY_EXIT = 0x400 + 1;

        private CalendarControl _calendarBoard = null;

        private bool _bLoadFinished = false;

        public KeyBoardArgs KeyBoardArgs
        {
            get { return _keyBoardArgs; }
            set { _keyBoardArgs = value; }
        }

        private KeyBoardArgs _keyBoardArgs;

        public DateTime SelectedDate
        {
            private set;
            get;
        }

        public FormCalender()
        {
            InitializeComponent();
            _calendarBoard = new CalendarControl();
            _calendarBoard.SelectedDateChanged += date_SelectedDatesChanged;
        }

        public FormCalender(KeyBoardArgs args) : this()
        {     
            SetKeyBoardArgs(args);
        }

        private void FormCalender_Load(object sender, EventArgs e)
        {                         
            elementHost1.Child = _calendarBoard;
            _bLoadFinished = true;
            //elementHost1.Dock = DockStyle.Fill;
        }

        public void SetKeyBoardArgs(KeyBoardArgs args)
        {
            _bLoadFinished = false;
            _keyBoardArgs = args;
            labelTitle.Text = _keyBoardArgs.Title.Replace('#', ' ');
            _calendarBoard.SetKeyBoardArgs(args);
            _bLoadFinished = true;
        }

        private void date_SelectedDatesChanged(object sender, SelectedDateChangedEventArgs e)
        {
            if (!_bLoadFinished)
            {
                return;
            }
            SelectedDate = e.SelectedDate;
            if (SelectedDate == null)
            {
                MessageBox.Show("选择的日期为空!");
                return;
            }
            if (_keyBoardArgs != null)
            {
                _keyBoardArgs.PassageType = SendMessageType.Response;
                int year = SelectedDate.Year;
                string month = SelectedDate.Month.ToString().PadLeft(2, '0');
                string day = SelectedDate.Day.ToString().PadLeft(2, '0');
                _keyBoardArgs.Content = string.Format("{0}-{1}-{2}", year, month, day);
                SendMessage();
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private bool SendMessage()
        {
            if (_keyBoardArgs.MsgReceiver == IntPtr.Zero)
            {
                MessageBox.Show("句柄错误", "句柄错误");
                return false;
            }
            int uMsg = (int)MessageType.WM_COPYDATA;
            string strMsgText = JsonHandler.ConvertToJsonString(_keyBoardArgs);

            byte[] data = System.Text.Encoding.UTF8.GetBytes(strMsgText);
            COPYDATASTRUCT_INTPTR cds = new COPYDATASTRUCT_INTPTR();
            cds.dwData = (IntPtr)uMsg;
            cds.cbData = data.Length;
            cds.lpData = Marshal.AllocHGlobal(data.Length);
            Marshal.Copy(data, 0, cds.lpData, data.Length);
            Message.SendMessage(_keyBoardArgs.MsgReceiver, uMsg, 0, ref cds);
            cds.Free();
            if (_keyBoardArgs.MsgReceiver != _keyBoardArgs.SenderHandle && _keyBoardArgs.SenderHandle != IntPtr.Zero)
            {
                // 通知发送者，程序即将关闭
                string strMsg = "Exit";
                int len = Encoding.Unicode.GetBytes(strMsg).Length;
                COPYDATASTRUCT dataTrans = new COPYDATASTRUCT();
                dataTrans.dwData = (IntPtr)MSG_NOTIFY_EXIT;
                dataTrans.cbData = len;
                dataTrans.lpData = strMsg;
                ComClassLibrary.Message.SendMessage(_keyBoardArgs.SenderHandle, MSG_NOTIFY_EXIT, 0, ref dataTrans);

            }
            return true;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            if (_keyBoardArgs == null)
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
                return;
            }
            int uMsg = (int)MessageType.WM_COPYDATA;
            _keyBoardArgs.PassageType = SendMessageType.Response;
            _keyBoardArgs.Content = "";
            _keyBoardArgs.Command = "Cancel";
            string strMsgText = JsonHandler.ConvertToJsonString(_keyBoardArgs);

            byte[] data = System.Text.Encoding.UTF8.GetBytes(strMsgText);
            COPYDATASTRUCT_INTPTR cds = new COPYDATASTRUCT_INTPTR();
            cds.dwData = (IntPtr)uMsg;
            cds.cbData = data.Length;
            cds.lpData = Marshal.AllocHGlobal(data.Length);
            Marshal.Copy(data, 0, cds.lpData, data.Length);
            ComClassLibrary.Message.SendMessage(_keyBoardArgs.MsgReceiver, uMsg, 0, ref cds);
            cds.Free();
            if (_keyBoardArgs.MsgReceiver != _keyBoardArgs.SenderHandle && _keyBoardArgs.SenderHandle != IntPtr.Zero)
            {
                // 通知发送者，程序即将关闭
                string strMsg = "Exit";
                int len = Encoding.Unicode.GetBytes(strMsg).Length;
                COPYDATASTRUCT dataTrans = new COPYDATASTRUCT();
                dataTrans.dwData = (IntPtr)MSG_NOTIFY_EXIT;
                dataTrans.cbData = len;
                dataTrans.lpData = strMsg;
                ComClassLibrary.Message.SendMessage(_keyBoardArgs.SenderHandle, MSG_NOTIFY_EXIT, 0, ref dataTrans);
            }
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
